package com.gxuwz.medical.domain.policy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DBUtil;

/**
 * 慢性病政策模块类
 * @author 沉羽墨玖
 *
 */
public class Policy {
	// 年份
	private String runYear; 
	// 封顶线
	private double maxline; 
	// 状态
	private String status; 
	// 备注
	private String remark; 
	//报销比例
	private double ratio;  

	public Policy() {

	}

	public Policy(String runYear, double maxline, String status, String remark,double ratio) {
		super();
		this.runYear = runYear;
		this.maxline = maxline;
		this.status = status;
		this.remark = remark;
		this.ratio = ratio;
	}
	/**
	 * 设置启用状态
	 * @param status
	 * @throws SQLException
	 */
	public void setup(String status)throws SQLException{
		Connection conn=null;
		this.status=status;
       try{
    	   //1:对象属性赋值,无
    	    conn=DBUtil.getConn();
    	   //2；调用方法更新状态
    	    updateStatus(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	private void updateStatus(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="update policy set status=? where runYear=?";
			 //创建执行带动态参数的SQL对象
			 pstmt=conn.prepareStatement(sql);
			 //设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.status);
			 pstmt.setString(index++, this.runYear);
			 int count=pstmt.executeUpdate();

		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	public void load(String runYear) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from policy where runYear=?");
			int index = 1;
			pstmt.setString(index, runYear);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				this.runYear = rs.getString("runYear");
				this.maxline = rs.getDouble("maxline");
				this.status = rs.getString("status");
				this.remark = rs.getString("remark");
				this.ratio = rs.getDouble("ratio");
			}
		} catch (SQLException e) {
			throw new SQLException("Policy with id " + runYear
					+ " could not be loaded from the database.");
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}

	}

	public void add() throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			saveToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			throw e;
		} finally {
			DBUtil.close(conn);
		}

	}

	private void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into policy");
			sqlBuff.append("(runYear,maxline,status,remark,ratio)");
			sqlBuff.append("values(?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.runYear);
			pstmt.setDouble(2, this.maxline);
			pstmt.setString(3, this.status);
			pstmt.setString(4, this.remark);
			pstmt.setDouble(5, this.ratio);
			pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DBUtil.close(pstmt);

		}

	}

	public void del(String runYear) throws SQLException {
		Connection conn = null;
		this.runYear = runYear;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}

	}

	private void deleteFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from policy where runYear=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.runYear);
			pstmt.executeUpdate();

		} catch (SQLException e) {
			throw new SQLException("Failed to delete record from table !", e);
		} finally {
			DBUtil.close(pstmt);
		}
	}

	public void edit() throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			editToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void editToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update policy t set t.maxline=?,t.status=?,t.remark=?,ratio=? where t.runYear=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setDouble(1, this.maxline);
			pstmt.setString(2, this.status);
			pstmt.setString(3, this.remark);
			pstmt.setDouble(4, this.ratio);
			pstmt.setString(5, this.runYear);
			pstmt.executeUpdate();

		} catch (SQLException e) {
			throw new SQLException("Failed to update policy  !", e);
		} finally {
			DBUtil.close(pstmt);
		}

	}

	public Policy(String runYear) throws Exception {
		load(runYear);
	}

	public String getRunYear() {
		return runYear;
	}

	public void setRunYear(String runYear) {
		this.runYear = runYear;
	}



	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public double getMaxline() {
		return maxline;
	}

	public void setMaxline(double maxline) {
		this.maxline = maxline;
	}

	public double getRatio() {
		return ratio;
	}

	public void setRatio(double ratio) {
		this.ratio = ratio;
	}

}
